<?php

namespace app\api\controller;

use app\common\exception\BusinessException;
use think\response\Json;

/**
 * 资料包
 */
class Material extends Base
{
    /**
     * getList
     * @throws BusinessException
     */
    public function getList(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'course_id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'course_id.require' => '课程id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }
        $list = app(\app\api\logic\Material::class)->getList($param);
        return $this->response($list);
    }

    /**
     * getInfo
     * @throws BusinessException
     */
    public function getInfo(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '资料id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        $info = app(\app\api\logic\Material::class)->getInfo($param);

        // 返回
        return $this->response($info);
    }

    /**
     * buy
     * @throws BusinessException
     */
    public function buy(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '资料id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        app(\app\api\logic\Material::class)->buy($param);

        // 返回
        return $this->response();
    }

    /**
     * shareRule
     * @return Json
     */
    public function shareRule(): Json
    {
        // 提交数据
        $data = app(\app\api\logic\Material::class)->shareRule();

        // 返回
        return $this->response($data);
    }

    /**
     * shareSave
     * @return Json
     * @throws BusinessException
     */
    public function shareSave(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'course_id' => 'require',
            'content' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'course_id.require' => '课程id不得为空',
            'content.require' => '提交内容不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        app(\app\api\logic\Material::class)->shareSave($param);

        // 返回
        return $this->response();
    }

    /**
     * getShareList
     * @throws BusinessException
     */
    public function shareList(): Json
    {
        // 提交查询
        $list = app(\app\api\logic\Material::class)->getShareList($this->request->param());

        // 返回
        return $this->response($list);
    }
}